//
//  LPWKNewsCommentHeaderView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/5.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPWKNewsCommentHeaderView.h"

@interface LPWKNewsCommentHeaderView ()

/** 头像 */
@property (nonatomic, strong) UIImageView *headerView;
/** name */
@property (nonatomic, strong) UILabel *name;
/** 时间 */
@property (nonatomic, strong) UILabel *time;
/** 收藏 */
@property (nonatomic, strong) UIButton *collect;
/** 评论 */
@property (nonatomic, strong) UIButton *comment;
/** 点赞 */
@property (nonatomic, strong) UIButton *like;
/** 评论内容 */
@property (nonatomic, strong) UILabel *contentLabel;
/** 下划线（可能没有   取决于子评论数组） */
@property (nonatomic, strong) UILabel *underLine;

@end

@implementation LPWKNewsCommentHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.headerView];
        [self.contentView addSubview:self.name];
        [self.contentView addSubview:self.time];
        [self.contentView addSubview:self.collect];
        [self.contentView addSubview:self.comment];
        [self.contentView addSubview:self.like];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.underLine];
        
        
        self.headerView.sd_layout
        .topSpaceToView(self.contentView, 15 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .widthIs(42 * [UIScreen zj_screenHScale])
        .heightEqualToWidth();
        
        self.headerView.sd_cornerRadius = @(self.headerView.width / 2.0);
        
        self.like.sd_layout
        .topEqualToView(self.headerView)
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .widthIs(150 * [UIScreen zj_screenWScale] / 3)
        .heightRatioToView(self.headerView, 0.6);
        
        self.comment.sd_layout
        .topEqualToView(self.like)
        .rightSpaceToView(self.like,0)
        .widthRatioToView(self.like, 1)
        .heightRatioToView(self.like, 1);
        
        self.collect.sd_layout
        .topEqualToView(self.like)
        .rightSpaceToView(self.comment,0)
        .widthRatioToView(self.like, 1)
        .heightRatioToView(self.like, 1);
        
        self.name.sd_layout
        .topEqualToView(self.headerView)
        .leftSpaceToView(self.headerView, 7 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.collect, 0)
        .heightRatioToView(self.collect, 1);
        
        self.time.sd_layout
        .topSpaceToView(self.name, 0)
        .leftEqualToView(self.name)
        .rightEqualToView(self.name)
        .bottomEqualToView(self.headerView);
        
        self.contentLabel.sd_layout
        .topSpaceToView(self.headerView, 10 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.name)
        .widthIs([UIScreen zj_screenWith] - 73 * [UIScreen zj_screenWScale])
        .heightIs(10);
        
        self.underLine.sd_layout
        .bottomSpaceToView(self.contentView, 0)
        .leftEqualToView(self.contentLabel)
        .rightEqualToView(self.contentLabel)
        .heightIs(1);
        
    }
    return self;
}


- (void)setModel:(ZJSCQusetionsSrectionModel *)model{
    _model = model;
    [self.headerView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL, model.PHOTO_THUMBUR]] placeholderImage:PlaceholderImage];
    [self.collect setTitle:[NSString stringWithFormat:@" %@", model.ATTENTIONCOUNT ] forState:UIControlStateNormal];
    [self.comment setTitle:[NSString stringWithFormat:@" %@", model.COMMENTCOUNT ] forState:UIControlStateNormal];
    [self.like setTitle:[NSString stringWithFormat:@" %@", model.PRAISECOUNT ] forState:UIControlStateNormal];
    
    self.name.text = model.USER_NAME;
    self.contentLabel.text = model.CONTENT;
    [self.contentLabel setLabelSpace:5 * [UIScreen zj_screenHScale]];
    
    self.contentLabel.sd_layout
    .topSpaceToView(self.headerView, 10 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.name)
    .widthIs([UIScreen zj_screenWith] - 73 * [UIScreen zj_screenWScale])
    .heightIs([self.contentLabel getSpaceLabelHeightFromSpace:5 * [UIScreen zj_screenHScale]]);
    
    [self.contentLabel updateLayout];
    
    self.time.text = model.CREATETIME;
    
    if (model.childModelArray.count == 0) {//没有评论
        [self cellNoComment:YES];
    }else{//有评论
        [self cellNoComment:NO];
    }
    //判断收藏。点赞情况(0点赞，1关注，2未点赞且关注  3点赞且关注)
    if ([model.ISCLICK isEqualToString:@"0"]) {
        self.like.selected = YES;
        self.collect.selected = NO;
    }else if ([model.ISCLICK isEqualToString:@"1"]) {
        self.like.selected = NO;
        self.collect.selected = YES;
    }else if ([model.ISCLICK isEqualToString:@"2"]) {
        self.like.selected = NO;
        self.collect.selected = NO;
    }
    else if ([model.ISCLICK isEqualToString:@"3"]) {
        self.like.selected = YES;
        self.collect.selected = YES;
    }

}

//有没有评论
- (void)cellNoComment:(BOOL)noComment{
    if (noComment) {//没有评论
        self.underLine.hidden = NO;
        
    }else{//有评论
        self.underLine.hidden = YES;
    }
    
}


#pragma mark - 点击事件
//收藏
- (void)action_collect:(UIButton *)button{
    if (button.selected) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self netCollectWithString:@"1" success:^{
        button.selected = YES;
        weakSelf.model.ATTENTIONCOUNT = [NSString stringWithFormat:@" %d",[weakSelf.model.ATTENTIONCOUNT integerValue] + 1];
        [button setTitle:weakSelf.model.ATTENTIONCOUNT forState:UIControlStateNormal];
        [weakSelf netWorkingSuccessWithString:@"1"];
    }];
}
//评论
- (void)action_comment:(UIButton *)button{
    if (self.delegate) {
        [self.delegate LPWKNewsCommentHeaderWithView:self model:self.model];
    }
}
//点赞
- (void)action_like:(UIButton *)button{
    if (button.selected) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self netCollectWithString:@"0" success:^{
        button.selected = YES;
        weakSelf.model.PRAISECOUNT = [NSString stringWithFormat:@"% ld",[weakSelf.model.PRAISECOUNT integerValue] + 1];
        [button setTitle:weakSelf.model.PRAISECOUNT forState:UIControlStateNormal];
        [weakSelf netWorkingSuccessWithString:@"0"];
    }];
}


//EVENTTYPE":0 点赞 1 关注
- (void)netCollectWithString:(NSString *)string success:(void(^)())success{
    NSDictionary *dict = @{@"STUDENTID":ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,@"EVENTID":self.model.ID,@"EVENTTYPE":string};
    [NetWorkingManager postWithUrlString:[NetWorkingURL praiseOrAttention] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//成功
            success();
        }
    } Fail:^(id object) {
        
    }];
}
//网络请求成功后改变本地数据  0 点赞 1 关注
- (void)netWorkingSuccessWithString:(NSString *)string{
    if ([string isEqualToString:@"0"]) {//点赞成功
        /** 0点赞，1关注，2未点赞且关注  3点赞且关注（当前登录人） */
        if ([self.model.ISCLICK isEqualToString:@"1"]) {//原来是关注状态
            self.model.ISCLICK = @"3";//改变成：点赞且关注
        }else{// 其他就为：2未点赞且关注
            self.model.ISCLICK = @"0";//改变成：点赞且关注
        }
    }else if ([string isEqualToString:@"1"]) {//关注成功
        /** 0点赞，1关注，2未点赞且关注  3点赞且关注（当前登录人） */
        if ([self.model.ISCLICK isEqualToString:@"0"]) {//原来是点赞状态
            self.model.ISCLICK = @"3";//改变成：点赞且关注
        }else{// 其他就为：2未点赞且关注
            self.model.ISCLICK = @"0";//改变成：点赞且关注
        }
    }
    
    if (self.delegate) {
        [self.delegate LPWKNewsCommentHeaderWithView:self model:self.model section:self.section];
    }
    
}


#pragma mark - Getters
- (UIImageView *)headerView{
    if (_headerView == nil){
        _headerView = [UIImageView new];
        _headerView.contentMode = UIViewContentModeCenter;
        
    }
    return _headerView;
}
- (UILabel *)contentLabel{
    if (_contentLabel == nil){
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont titleTextFont];
        _contentLabel.textColor = [UIColor textBlackColor];
        _contentLabel.numberOfLines = 0;
        
    }
    return _contentLabel;
}
- (UILabel *)name{
    if (_name == nil){
        _name = [UILabel new];
        _name.textColor = [UIColor textBlackColor];
        _name.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        
    }
    return _name;
}
- (UILabel *)time{
    if (_time == nil){
        _time = [UILabel new];
        _time.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _time.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
        
    }
    return _time;
}

- (UIButton *)collect{
    if (_collect == nil){
        _collect = ({
            UIButton *button = [UIButton new];
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            button.titleLabel.font = [UIFont titleTextFont];
            [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_shoucang_d"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_shoucang_h"] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(action_collect:) forControlEvents:UIControlEventTouchUpInside];
            
            button;
        });
    }
    return _collect;
}

- (UIButton *)comment{
    if (_comment == nil){
        _comment = ({
            UIButton *button = [UIButton new];
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
            button.titleLabel.font = [UIFont titleTextFont];
            [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_com"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(action_comment:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _comment;
}
- (UIButton *)like{
    if (_like == nil){
        _like = ({
            UIButton *button = [UIButton new];
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
            button.titleLabel.font = [UIFont titleTextFont];
            [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_zan_d"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_zan_h"] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(action_like:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _like;
}

- (UILabel *)underLine{
    if (_underLine == nil){
        _underLine = [UILabel new];
        _underLine.backgroundColor = [UIColor zj_colorWithHexString:@"ededed"];
    }
    return _underLine;
}

@end
